package com.atguigu.yygh.cmn.controller;


import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.utils.R;
import com.atguigu.yygh.model.cmn.Dict;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author Dufeifei
 * @since 2024-06-08
 */

//@CrossOrigin
@RestController
@RequestMapping("/admin/cmn/dict")
@Api(tags = "数据字典")


public class DictController {

    @Autowired
    private DictService dictService;


    @ApiOperation("根据value 与code 查询名称")
    @GetMapping("/getByValueAndCode/{value}/{code}")
    public String getByValueAndCode(@ApiParam(value = "value值",required = true)@PathVariable Long value,
                                    @ApiParam(value = "dict_code",required = true)@PathVariable String code) {

        //根据dict_code 获取ID
        QueryWrapper<Dict> dictQueryWrapper1 = new QueryWrapper<>();
        dictQueryWrapper1.eq("dict_code",code);
        Long parentId = dictService.getOne(dictQueryWrapper1).getId();

        //根据parent_id 与value 查询具体的名称
        QueryWrapper<Dict> dictQueryWrapper2 = new QueryWrapper<>();
        dictQueryWrapper2.eq("parent_id",parentId);
        dictQueryWrapper2.eq("value",value);

        return dictService.getOne(dictQueryWrapper2).getName();

    }

    @ApiOperation("根据value 查询名称")
    @GetMapping("/findNameByValue/{value}")
    public String findNameByValue(@ApiParam(value = "value值",required = true)@PathVariable Long value) {

        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("value",value);
        return dictService.getOne(dictQueryWrapper).getName();

    }
    @ApiOperation("根据code 查询子数据")
    @GetMapping("/getByCode/{code}")
    public R getByCode(@ApiParam(value = "code",required = true)@PathVariable String code){
        //根据dict_code 获取ID
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("dict_code",code);
        Long parentId = dictService.getOne(dictQueryWrapper).getId();

        //根据parent_Id 查询子数据
        QueryWrapper<Dict> dictQueryWrapper2 = new QueryWrapper<>();
        dictQueryWrapper2.eq("parent_id",parentId);
        List<Dict> list = dictService.list(dictQueryWrapper2);

        return R.ok().data("list",list);

    }


    @ApiOperation("查询子数据")
    @GetMapping("/findChildren/{parentId}")
    public R findChildren(@ApiParam(value = "父ID",required = true)@PathVariable Long parentId){

    List <Dict> dictList=dictService.findChildren(parentId);

    return R.ok().data("list",dictList);
    }

    @ApiOperation("导出数据")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }

    @ApiOperation(value = "导入")
    @PostMapping("importData")
    public R importData(MultipartFile file) {
        dictService.importDictData(file);
        return R.ok();
    }


}

